﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using ShootLife2D.Helpers;

namespace ShootLife2D.Screens
{
    public class LoadingScreen : Screen
    {
        private Game1 game;
        private SpriteFont _font;
        private SpriteBatch _spriteBatch;
        private Texture2D _rectangleTexture;
        private double percentComplete = 0.0d;
        private Dictionary<string, Type> resToLoad;
        private int counter = 0;
        private string gameScreen;
        public LoadingScreen(Game1 game, string nextScreen, Dictionary<string, Type> resourcesToLoad)
        {
            //загрузка анимации загрузки и т.п. далее в апдейте если nextScreen null тогда подгрузить его и затем сменить
            //TODO: сделать загрузчик ресурсов и по хорошему указывать в параметре не сам экран, и его тип, для дальнейшей динамической подгрузки
            this.game = game;
            resToLoad = resourcesToLoad;
            gameScreen = nextScreen;
        }
        public override void LoadContent(ContentManager content)
        {
            _font = Game1.Resources.GetResource("MainFont", typeof(SpriteFont)) as SpriteFont;
            _spriteBatch = new SpriteBatch(game.GraphicsDevice);
            _rectangleTexture = GraphicsPrimitivesHelper.CreateFilledRectangle(10, 10, Color.Red, game.GraphicsDevice);
            if (resToLoad != null)
            {
                Thread tr = new Thread(LoadResources);
                tr.Start();
            }
            base.LoadContent(content);
        }
        void LoadResources()
        {
            foreach (KeyValuePair<string, Type> keyValuePair in resToLoad)
            {
                Game1.Resources.GetResource(keyValuePair.Key, keyValuePair.Value);
                counter++;
            }
        }
        public override void Update(GameTime gameTime)
        {
            CheckPercentage();
            if (percentComplete >= 1.0f)
            {
                switch (gameScreen.ToLower())
                {
                    case "main":
                        break;
                    default:
                        break;
                }
            }
            base.Update(gameTime);
        }
        public override void Draw(GameTime gameTime)
        {
            Viewport vp = game.GraphicsDevice.Viewport;
            _spriteBatch.Begin();
            string loadString = string.Format("Loading... {0}%",
                                              (int)(percentComplete * 100) + 1);
            Vector2 maxLoadBarSize = _font.MeasureString("Loading... 100%");
            _spriteBatch.DrawString(_font, loadString, new Vector2(vp.Width / 2, vp.Height / 2), Color.Red, 0f, _font.MeasureString(loadString) / 2, 1f, SpriteEffects.None, 1f);
            _spriteBatch.Draw(_rectangleTexture, new Vector2(vp.Width / 2 - (maxLoadBarSize.X / 2), (vp.Height / 2) + 40), null, Color.Red, 0f, new Vector2(0f), new Vector2(maxLoadBarSize.X * (float)percentComplete / 10.0f, 1), SpriteEffects.None, 1.0f);
            _spriteBatch.End();
            base.Draw(gameTime);
        }
        public void CheckPercentage()
        {
            //Game1 g = game as Game1;
            //double hdPercentage = 1.0d;
            //if (g.MaximumHDTextures > 0)
            //{
            //    hdPercentage = (double)g.CurrentHDTextures/(double)g.MaximumHDTextures;
            //}
            if (resToLoad == null)
            {
                percentComplete = 1.0f;
            }
            else
            {
                percentComplete = (double)counter / (double)resToLoad.Count;
            }
        }
    }
}
